using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Customer;
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;


namespace WindowsFormsApplication1
{
    public partial class TypeSelection : Form
    {
        private string cardnumber;
        private Person person;
        BitArray status;
        UsernamePassword userName;
        bool dispenseAllowed;
        

        public TypeSelection(Person Person, BitArray Status)
        {
            person = Person;
            status = Status;
            cardnumber = person.CardNumber;
            dispenseAllowed = false;
            
            InitializeComponent();
            retryButton.Visible = false;
            //configuring the serial port            
            serialPort1.PortName = "COM6";
            serialPort1.BaudRate = 9600;
            serialPort1.DataBits = 8;
            serialPort1.Parity = Parity.None;
            serialPort1.StopBits = StopBits.One; 
        }

        private void mixedDrinkButton_Click(object sender, EventArgs e)
        {
            // show the mixed drink form
            this.Close();         
            MixedDrinkForm mixedDrinkForm = new MixedDrinkForm(person, status);
            mixedDrinkForm.ShowDialog();
        }

        private void pourYourOwnButton_Click(object sender, EventArgs e)
        {
            tryConnection();

            if (dispenseAllowed)
            {

                // set bits 1 and 2 of the status to say its a new order and start pouring
                status.Set(1, true);
                status.Set(2, true);
                this.Close();
                BeveragePourType beerPourType = new BeveragePourType(person, status);
                serialPort1.Close();
                beerPourType.ShowDialog();
            }
            else
            {
                //MessageBox.Show("error");
            }
         
        }
        
        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void phoneAppButton_Click(object sender, EventArgs e)
        {
            // open phone app form            
            userName = new UsernamePassword(person, status);
            this.Close();
            userName.ShowDialog();           
        }
        public void tryConnection()
        {
            try
            {
                if (serialPort1.IsOpen)
                {
                    serialPort1.Close();
                }
                serialPort1.Open();
                dispenseAllowed = true;
                dispenseLabel.Visible = false;
                retryButton.Visible = false;
                sorryLabel.Visible = false;

            }
            catch (Exception ex)
            {
                dispenseAllowed = false;
                dispenseLabel.Visible = true;
                retryButton.Visible = true;
                sorryLabel.Visible = true;             
            }
        }

        private void helpButton_Click(object sender, EventArgs e)
        {
            string helpMessage = "On this form you must choose to either order a " +
                "mixed drink which will then be sent to the bartender to be made " +
                "or to pour your own beer.";
            HelpForm helpForm = new HelpForm(helpMessage);
            helpForm.ShowDialog();

        }

        private void retryButton_Click(object sender, EventArgs e)
        {
            tryConnection();
        }
    }
}
